import Request from 'utils/request';
import SystemUtil from 'utils/systemUtil';

import CartInfo from 'models/shoppingCart/cartInfo';

export default class ShoppingCartApi {
  static getShopCartData(lnglat) {
    const apiName = 'api/wehcatshop/getBuyCarInfo';

    return Request.post(apiName, {
      longitude: lnglat.lng,
      latitude: lnglat.lat
    }).then(res => {
      let data = res.data;
      if (SystemUtil.isEmpty(data)) {
        data = [];
      } else {
        data = data.rows;
        data = Object.values(data);
      }
      data = new CartInfo(data);
      return data;
    });
  }

  static updateBuyCount(goods) {
    const apiName = 'api/wehcatshop/updateBuyCarLog';

    return Request.post(apiName, {
      buyCarLogId: goods.id,
      buyCount: goods.buyCount
    }).then(res => {
      return res.msg;
    });
  }

  static deleteGoods(goods) {
    const apiName = 'api/wehcatshop/updateBuyCarLog';

    return Request.post(apiName, {
      buyCarLogId: goods.id,
      status: 0
    }).then(res => {
      return res.msg;
    });
  }
}
